// pages/component/timeItem/timeItem.js
const app = getApp();

const api = require('../../../apis/request.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    timing: {
      type: Object,
      value: undefined
    },
    isTiming:{
      type:Boolean,
      value:false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    myDisabled:true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    timeDeleteTab:function(event){
      // console.log("点击删除按钮  " + JSON.stringify(event));
      var _this=this;
      var e = {
        success: true,
        data: _this.data.timing
      }
      _this.triggerEvent('itemDelete', e, '');
    },
    timingUsingCheck: function (event) {
      var _this = this;
      // console.log("是否启用事件详情:"+JSON.stringify(event));
      var timing = _this.data.timing;
      timing.isTiming = event.detail.value.length != 0;
      _this.setData({ timing: timing });
      _this.changeDisableStatu(false);

    },
    bindStartTimeChange: function (event) {
      var _this = this;
      // console.log('picker发送选择改变，携带值为', event.detail.value)
      var timing = _this.data.timing;
      timing.startTime = event.detail.value;
      _this.setData({ timing: timing });
      _this.changeDisableStatu(false);
      // this.setData({
      //   time: event.detail.value
      // });
    },
    bindEndTimeChange: function (event) {
      var _this = this;
      // console.log('picker发送选择改变，携带值为', event.detail.value)
      var timing = _this.data.timing;
      timing.endTime = event.detail.value;
      _this.setData({ timing: timing });
      _this.changeDisableStatu(false);
      // this.setData({
      //   time: event.detail.value
      // });
    },
    save:function(event){
      console.log("保存");
      var _this = this;
      var timing = _this.data.timing;
      var startTime = timing.startTime;
      var endTime = timing.endTime;
      timing.startTime = "1970-01-01 " + startTime + ":00";
      timing.endTime = "1970-01-01 " + endTime + ":00";
        console.log("保存的timing对象 ： "+JSON.stringify(timing));
  
      api.updateTiming(JSON.stringify(timing), function (res) {
        // console.log(JSON.stringify(res));

        //1970-01-01 00:00:00
        timing.startTime = startTime;
        timing.endTime = endTime;

        _this.changeDisableStatu(true);

      });

      // wx.request({

      //   url: app.globalData.URL + "/timing/update",
      //   header: {
      //     "Accept": "application/json",
      //     "Content-Type": "application/json",
      //     "token": app.globalData.token
      //   },
      //   method: "PUT",
      //   data: JSON.stringify(timing),
      //   complete: function (res) {
      //     // console.log(JSON.stringify(res));
          
      //     //1970-01-01 00:00:00
      //     timing.startTime = startTime;
      //     timing.endTime = endTime;

      //     _this.changeDisableStatu(true);

      //   }
      // });

     
    },
    /**
     * 是否可以点击保存按钮？只有编辑过该条目后，其保存按钮才可点击。保存成功后,将再次变为不可点击
     */
    changeDisableStatu: function (myDisabled){
      var _this=this;
      // console.log("是否可点击： " + myDisabled);
      _this.setData({ myDisabled: myDisabled});
    }
  },

  lifetimes: {
    ready:function(){
      var _this = this;
      console.log(this.data.timing)
      // console.log("按钮状态 "+_this.data.myDisabled);
    }
  }


})
